export const query2str = (queryObj: Record<string, string>, encodedFields: string[] = []) => {
  return Object.entries(queryObj).reduce((str, [key, value], idx, arr) => {
    const tail = idx !== arr.length - 1 ? '&' : ''

    const _val = encodedFields.includes(key) ? encodeURIComponent(value) : value

    return (str += `${key}=${_val}${tail}`)
  }, '')
}

export const whitespace2kebab = (withWhitespace: string) => {
  return withWhitespace.replace(/\s/g, '-').toLocaleLowerCase()
}

export const baseWalk = <T>(items: T[], callback: (item: T) => void) => {
  items.forEach((item) => {
    callback(item)
    Array.isArray((item as any).children) && baseWalk((item as any).children, callback)
  })
}

export const capitalize = (str: string) => {
  return str.charAt(0).toUpperCase() + str.slice(1).toLocaleLowerCase()
}

export const randomId = () => Math.random().toString(36).substring(2, 9)
